package com.scaffold.upload.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wangjg.commons.page.Page;

import com.scaffold.upload.dao.FileInfoDAO;
import com.scaffold.upload.entity.FileInfo;

@Service
@Transactional(rollbackFor = Exception.class)
public class FileInfoService {

	@Autowired
	private FileInfoDAO fileInfoDAO;

	@Transactional(readOnly=true)
	public FileInfo getFileInfo(Long id ) {
		return this.fileInfoDAO.findById(id);
	}
	
	@Transactional(readOnly=true)
	public FileInfo findByPath(String filePath) {
		return this.fileInfoDAO.findByProperty("filePath", filePath);
	}
	
	@Transactional(readOnly=true)
	public FileInfo findByHashCode(String hashCode) {
		return this.fileInfoDAO.findByProperty("hashCode", hashCode);
	}
	
	@Transactional(readOnly=true)
	public List<FileInfo> findLinked(Long linkId) {
		return this.fileInfoDAO.findListByProperty("linkId", linkId);
	}
	
	public void save(FileInfo fileInfo){
		Date now = new Date();
		if(fileInfo.getId()==null){
			fileInfo.setCreated(now);
			fileInfo.setModified(now);
			this.fileInfoDAO.save(fileInfo);
		}else{
			fileInfo.setModified(now);
			this.fileInfoDAO.update(fileInfo);
		}
	}

	public void update(FileInfo fileInfo){
		Date now = new Date();
		fileInfo.setModified(now);
		this.fileInfoDAO.update(fileInfo);
	}

	public void delete(FileInfo fileInfo){
		this.fileInfoDAO.delete(fileInfo);
	}
	
	@Transactional(readOnly=true)
	public List<FileInfo> pageQuery(Page page, String contentType, String temp, String keyword) {
		return this.fileInfoDAO.pageQuery(page, contentType, temp, keyword);
	}

}
